<!DOCTYPE html>
<html>

<head>
    <title>v-for实例</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0 " />
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <h1>v-for实例</h1>
    <hr>
    <div id="app">
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
            <hr>
            <ul>
                <li v-for="(student,index) in sortStudent">
                    {{index+1}}:{{student.name}}--{{student.age}}
                </li>
            </ul>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                items: [55, 23, 7, 14, 54, 32, 28],
                students: [{
                    name: 'LiuYue',
                    age: 33
                }, {
                    name: 'LYue',
                    age: 20
                }, {
                    name: 'LiuY',
                    age: 25
                }, {
                    name: 'LY',
                    age: 16
                }, ]
            },
            computed: {

                sortItems: function() {
                    return this.items.sort(sortNumber);
                },
                sortStudent: function() {
                    return sortByKey(this.students, 'age')
                }
            }


        })

        function sortNumber(a, b) {
            return a - b;
        }

        function sortByKey(array, key) {
            return array.sort(function(a, b) {
                var x = a[key];
                var y = b[key];
                return ((x < y) ? -1 : ((x > y) ? 1 : 0));
            });
        }
    </script>
</body>